<template>
  <!-- // 分配角色 -->
  <div>
    <!-- 这里将来会放置多选列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="doSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employee'

export default {
  props: {
    assignRoleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 选择之后,存入的id
      roleIds: [],
      // 角色列表
      roleList: []
    }
  },
  created() {
    this.loadRoles()
    this.loadEmployee()
  },
  methods: {
    closeDialog() {
      this.$emit('close')
    },
    // 加载角色信息
    async loadRoles() {
      const { data: res } = await getRoles({ page: 1, pagesize: 1000 })
      // console.log('角色数据....', res)
      this.roleList = res.data.rows
    },
    // 通过id加载员工信息
    async loadEmployee() {
      const { data: res } = await getUserDetailById(this.assignRoleId)
      // console.log('id员工信息', res)
      this.roleIds = res.data.roleIds
    },
    // 保存信息
    async doSave() {
      const { data: res } = await assignRoles({ id: this.assignRoleId, roleIds: this.roleIds })
      // console.log('给员工分配角色', res)
      this.$message.success(res.message)
      // 提示信息
      this.$emit('success')
    }
  }
}
</script>
